package com.inputabc.ct.v1.ui.border;

import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

/**
 * 经典终端风格边框
 */
public class TerminalBorder extends AbstractBorder {
    private static final int BORDER_THICKNESS = 1;
    private static final int INSET = 8;
    // 调整圆角半径为适中大小，不过于圆滑
    private static final int CORNER_RADIUS = 30;
    
    private final Color borderColor = new Color(50, 130, 240);  // 中蓝色
    private Color backgroundColor; // 背景色，从外部传入
    
    // 缓存的边框图像，避免每次重绘
    private BufferedImage cachedBorder = null;
    private int cachedWidth = 0;
    private int cachedHeight = 0;
    
    /**
     * 创建默认黑色背景的终端边框
     */
    public TerminalBorder() {
        this(new Color(20, 20, 20, 220)); // 默认使用半透明黑色背景
    }
    
    /**
     * 创建指定背景色的终端边框
     * @param backgroundColor 背景颜色
     */
    public TerminalBorder(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
        
        // 确保背景颜色有一定透明度，以保持边框效果美观
        if (backgroundColor.getAlpha() == 255) {
            // 如果是完全不透明的颜色，添加一些透明度
            this.backgroundColor = new Color(
                backgroundColor.getRed(),
                backgroundColor.getGreen(),
                backgroundColor.getBlue(),
                220); // 设置透明度为220
        }
    }
    
    /**
     * 设置背景颜色
     * @param backgroundColor 新的背景颜色
     */
    public void setBackgroundColor(Color backgroundColor) {
        // 确保背景颜色有一定透明度
        if (backgroundColor.getAlpha() == 255) {
            this.backgroundColor = new Color(
                backgroundColor.getRed(),
                backgroundColor.getGreen(),
                backgroundColor.getBlue(),
                220); // 设置透明度为220
        } else {
            this.backgroundColor = backgroundColor;
        }
        
        // 重置缓存，以便下次重绘
        cachedBorder = null;
    }
    
    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        // 如果尺寸改变或缓存为空，重新绘制边框
        if (cachedBorder == null || cachedWidth != width || cachedHeight != height) {
            cachedWidth = width;
            cachedHeight = height;
            
            // 创建一个带Alpha通道的图像，尺寸比实际大一些以避免边界问题
            cachedBorder = new BufferedImage(width + 8, height + 8, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = cachedBorder.createGraphics();
            
            // 设置高质量渲染提示
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            
            // 应用4像素的平移，避免边缘裁剪问题
            g2d.translate(4, 4);
            
            // 确保背景透明
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(-8, -8, width + 16, height + 16);
            g2d.setComposite(AlphaComposite.SrcOver);
            
            // 创建稍微小一些的填充区域，避免边缘露出
            int fillPadding = 1;
            g2d.setColor(backgroundColor);
            g2d.fill(new RoundRectangle2D.Float(
                    fillPadding, 
                    fillPadding, 
                    width - 2*fillPadding, 
                    height - 2*fillPadding, 
                    CORNER_RADIUS, 
                    CORNER_RADIUS));
            
            // 设置边框颜色
            g2d.setColor(borderColor);
            
            // 使用圆形端点和连接点的笔画，使边框更圆滑
            g2d.setStroke(new BasicStroke(BORDER_THICKNESS, 
                    BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            
            // 画外框 - 使用适中圆角的矩形，确保完全在填充区域内部
            g2d.draw(new RoundRectangle2D.Float(
                    fillPadding, 
                    fillPadding, 
                    width - 2*fillPadding - 1, 
                    height - 2*fillPadding - 1, 
                    CORNER_RADIUS, 
                    CORNER_RADIUS
            ));
            
            // 内边距调整，充分适应圆角
            int innerPadding = CORNER_RADIUS / 2;
            
            // 绘制顶部和底部的水平线
            int yTop = INSET;
            int yBottom = height - INSET - 3;
            int horizontalInset = CORNER_RADIUS / 2 + innerPadding; // 水平线内缩
            
            // 绘制圆滑水平线，使用更粗的线条
            g2d.setStroke(new BasicStroke(BORDER_THICKNESS + 0.5f, 
                    BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g2d.drawLine(horizontalInset, yTop, width - 2*fillPadding - horizontalInset - 1, yTop);
            g2d.drawLine(horizontalInset, yBottom, width - 2*fillPadding - horizontalInset - 1, yBottom);
            
            // 使用更圆的圆点装饰
            int dotSize = 8;
            
            // 调整圆点位置，确保在圆角内部
            int dotOffset = CORNER_RADIUS / 2;
            
            // 四个角的圆点
            g2d.fillOval(dotOffset, dotOffset, dotSize, dotSize);
            g2d.fillOval(width - 2*fillPadding - dotSize - dotOffset, dotOffset, dotSize, dotSize);
            g2d.fillOval(dotOffset, height - 2*fillPadding - dotSize - dotOffset, dotSize, dotSize);
            g2d.fillOval(width - 2*fillPadding - dotSize - dotOffset, height - 2*fillPadding - dotSize - dotOffset, dotSize, dotSize);
            
            // 添加水平线上的圆点装饰，注意位置要充分内缩
            int centerDotOffset = dotSize / 2;
            g2d.fillOval(width / 3 - centerDotOffset, yTop - centerDotOffset, dotSize, dotSize);
            g2d.fillOval(2 * width / 3 - centerDotOffset, yTop - centerDotOffset, dotSize, dotSize);
            g2d.fillOval(width / 3 - centerDotOffset, yBottom - centerDotOffset, dotSize, dotSize);
            g2d.fillOval(2 * width / 3 - centerDotOffset, yBottom - centerDotOffset, dotSize, dotSize);
            
            g2d.dispose();
        }
        
        // 绘制缓存的边框图像，偏移-4像素以适应之前的平移
        g.drawImage(cachedBorder, x - 4, y - 4, null);
    }
    
    @Override
    public Insets getBorderInsets(Component c) {
        // 适当调整边距以适应圆角
        return new Insets(INSET + 12, INSET + 12, INSET + 12, INSET + 12);
    }
    
    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        // 适当调整边距以适应圆角
        insets.top = INSET + 12;
        insets.left = INSET + 12;
        insets.bottom = INSET + 12;
        insets.right = INSET + 12;
        return insets;
    }
    
    @Override
    public boolean isBorderOpaque() {
        // 设置为不透明，确保完全覆盖
        return true;
    }
} 